<?php

namespace app\store\controller\goods;

use app\store\controller\Controller;
use app\store\model\Category as CategoryModel;
use app\store\model\Lesson as LessonModel;
use app\store\model\LessonDetails;
use think\Cache;

/**
 * 内容
 * Class Contents
 * @package app\store\controller\goods
 */
class Contents extends Controller
{
    /**
     * 课程列表
     * @return mixed
     */
    public function index()
    {
        $data = $this->request->param();
        $lesson_id = $data['lesson_id'];
        $catgory = [];
        $model = new LessonDetails();
        $list = $model->getList($lesson_id);
        return $this->fetch('index', compact('list','lesson_id', 'catgory'));
    }

    /**
     * 删除课程
     * @param $details_id
     * @return array|bool
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function delete($details_id)
    {
        $model = LessonDetails::get($details_id);
        if (!$model->remove($details_id)) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    /**
     * 添加课程内容
     * @return array|mixed
     */
    public function add()
    {
        $type = $this->request->param('type');
        $lessonid = $this->request->param('lesson_id');
        $model = new LessonDetails();
        if (!$this->request->isAjax()) {
            $template = 'add'.$type;
            return $this->fetch($template, compact('catgory','lessonid','type'));
        }
        // 新增记录
        if ($model->add($this->postData())) {
            return $this->renderSuccess('添加成功', url('goods.contents/index',['lesson_id'=>$lessonid]));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑课程
     * @param $lesson_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $type = $this->request->param('type');
        $details_id = $this->request->param('details_id');
        $lesson_id = $this->request->param('lesson_id');
        if (!$this->request->isAjax()) {
            // 获取所有地区
            $model = LessonDetails::get($details_id);
            $template = 'edit'.$type;
            return $this->fetch($template, compact('model'));
        }
        $model = new LessonDetails();
        // 更新记录
        if ($model->edit($this->postData())) {
            return $this->renderSuccess('更新成功', url('goods.contents/index',['lesson_id'=>$lesson_id]));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

}
